﻿using System.Linq;
using System.Net;
using System.Threading;
using Gems;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GemsTest
{
    [TestClass]
    public class UdpUnicastLinkTests
    {
        [TestMethod]
        public void JustWorks()
        {
            using (var link = new UdpUnicastLink(5435))
            {
                bool received=false;
                byte[] buf1 =  {1,2,3};
                byte[] buf2 = null;
                link.Received += delegate(byte[] data)
                                     {
                                         received = true;
                                         buf2 = data;
                                     };

                link.Send("localhost", buf1);

                var stopwatch = new System.Diagnostics.Stopwatch();
                stopwatch.Start();
                do
                {
                    Thread.SpinWait(100);
                } 
                while (!received && stopwatch.ElapsedMilliseconds < 3000);

                foreach (var x in buf1.Zip(buf2,(b1,b2)=>new {A=b1, B =b2})) Assert.AreEqual(x.A, x.B);
            }
        }
    }
}